package com.btkjgc.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.btkjgc.framework.BasicController;
import com.btkjgc.model.Form;
import com.btkjgc.model.SearchForm;
import com.btkjgc.model.UpdateForm;
import com.btkjgc.service.impl.LeftBarManager_Impl;
import com.btkjgc.service.intf.RoleManageService_Intf;

@Controller
public class RoleManageController extends BasicController{
	static Logger logger = Logger.getLogger(RoleManageController.class);
	
	@Autowired
	private RoleManageService_Intf roleManageService;
	
	public static class EntitySearchForm extends SearchForm {
		
	}
	
	public static class RoleInsertForm extends Form {
		private String name;
		
		private Integer level;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getLevel() {
			return level;
		}

		public void setLevel(Integer level) {
			this.level = level;
		}
		
	}
	
	public static class RoleUpdateForm extends UpdateForm {
		private String name;
		
		private Integer level;
		
		private Integer editid;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public Integer getLevel() {
			return level;
		}

		public void setLevel(Integer level) {
			this.level = level;
		}

		public Integer getEditid() {
			return editid;
		}

		public void setEditid(Integer editid) {
			this.editid = editid;
		}
		
	}
	
	@RequestMapping(value = "role_list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView process(HttpServletRequest request) {
		ModelAndView mv = super.process(request);
		
		return mv;
	}
	
	public void list(HttpServletRequest request, ModelAndView mv,
			EntitySearchForm form){
		mv.setViewName("role_list");
		String postaction = request.getParameter("postaction");
		if (postaction != null && postaction.equals("edit")) {
			String id = request.getParameter("editid");
			String name = request.getParameter("name");
			String level = request.getParameter("level");
			roleManageService.updateRole(name,level,id);
		}
		if (postaction != null && postaction.equals("batchDel")) {
			String[] id = request.getParameterValues("checklist");
			roleManageService.batchDel(id);
		}
		
		mv.addObject("role_list", roleManageService.getRoleList());
		
		mv.addObject("level_list", roleManageService.getLevelList());
		
	}
	
	public void insert(HttpServletRequest request, ModelAndView mv,
			RoleInsertForm form){
		roleManageService.insertRole(request, form.getName(),
				form.getLevel());
		request.getSession().setAttribute("left_menu",
				(new LeftBarManager_Impl()).getLeftMenuByRoleId(5));
	}
	
	public void delete(HttpServletRequest request, ModelAndView mv, String id){
		roleManageService.deleteById(id);
	}
	
}